﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace animationTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        System.Timers.Timer timer = new System.Timers.Timer(10);
        const int MAX_BUTTERFLYFRAME = 7;
        int currentButterflyFrame = 0;
        const string butterflyImageNameStart = "/animationTest;component/Images/butterfly_Frame";
        const string butterflyImageNameFormat = ".png";
        public MainWindow()
        {
            InitializeComponent();

            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
            {
                //winformsHost.Margin = new Thickness(winformsHost.Margin.Top+10);
                if (currentButterflyFrame < MAX_BUTTERFLYFRAME)
                {
                    currentButterflyFrame++;
                    string imageName = butterflyImageNameStart + currentButterflyFrame.ToString() + butterflyImageNameFormat;
                    butterflyBox.Source = new BitmapImage(new Uri(imageName, UriKind.Relative));
                    butterflyBox2.Source = new BitmapImage(new Uri(imageName, UriKind.Relative));
                    butterflyBox3.Source = new BitmapImage(new Uri(imageName, UriKind.Relative));

                    int randomLeft = RandomNumber(1, 10);
                    int randomTop = RandomNumber(1, 10);
                    butterflyBox.Margin = new Thickness(butterflyBox.Margin.Left - randomLeft, butterflyBox.Margin.Top - randomTop, 0, 0);

                    randomLeft = RandomNumber(1, 2);
                    randomTop = RandomNumber(2, 8);
                    butterflyBox2.Margin = new Thickness(butterflyBox2.Margin.Left - randomLeft, butterflyBox2.Margin.Top - randomTop, 0, 0);

                    randomLeft = RandomNumber(3, 10);
                    randomTop = RandomNumber(1, 3);
                    butterflyBox3.Margin = new Thickness(butterflyBox3.Margin.Left - randomLeft, butterflyBox3.Margin.Top - randomTop, 0, 0);
                }
                else
                {
                    resetButterflyImage();
                }
                if (butterflyBox.Margin.Top <= (0 - butterflyBox.Height) || butterflyBox.Margin.Left <= (0 - butterflyBox.Width))
                {
                    butterflyBox.Margin = new Thickness(405, 239, 0, 0);
                }

                if (butterflyBox2.Margin.Top <= (0 - butterflyBox2.Height) || butterflyBox2.Margin.Left <= (0 - butterflyBox2.Width))
                {
                    butterflyBox2.Margin = new Thickness(405, 239, 0, 0);
                }

                if (butterflyBox3.Margin.Top <= (0 - butterflyBox3.Height) || butterflyBox3.Margin.Left <= (0 - butterflyBox3.Width))
                {
                    butterflyBox3.Margin = new Thickness(405, 239, 0, 0);
                }
            }));
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        private void resetButterflyImage()
        {
            string imageName = butterflyImageNameStart + "0" + butterflyImageNameFormat;
            butterflyBox.Source = new BitmapImage(new Uri(imageName, UriKind.Relative));
            butterflyBox2.Source = new BitmapImage(new Uri(imageName, UriKind.Relative));
            butterflyBox3.Source = new BitmapImage(new Uri(imageName, UriKind.Relative));
            currentButterflyFrame = 0;
        }
    }
}
